package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.PmsAlbumMapper;
import com.ruoyi.mall.domain.PmsAlbum;
import com.ruoyi.mall.service.IPmsAlbumService;
import org.springframework.util.StringUtils;

/**
 * 商品相册Service业务层处理
 * 
 * @author fdk
 * @date 2023-09-09
 */
@Service
public class PmsAlbumServiceImpl implements IPmsAlbumService 
{
    @Autowired
    private PmsAlbumMapper pmsAlbumMapper;

    /**
     * 查询商品相册
     * 
     * @param id 商品相册主键
     * @return 商品相册
     */
    @Override
    public PmsAlbum selectPmsAlbumById(Long id)
    {
        return pmsAlbumMapper.selectPmsAlbumById(id);
    }

    /**
     * 查询商品相册列表
     * 
     * @param pmsAlbum 商品相册
     * @return 商品相册
     */
    @Override
    public List<PmsAlbum> selectPmsAlbumList(PmsAlbum pmsAlbum)
    {
        return pmsAlbumMapper.selectPmsAlbumList(pmsAlbum);
    }

    /**
     * 新增商品相册
     * 
     * @param pmsAlbum 商品相册
     * @return 结果
     */
    @Override
    public int insertPmsAlbum(PmsAlbum pmsAlbum)
    {
        String albumName = pmsAlbum.getName();
        if (StringUtils.isEmpty(albumName)){
            throw new RuntimeException("相册名称不能为空，请指定商品品牌名称");
        }

        int rows = pmsAlbumMapper.selectPmsAlbumByName(albumName);
        if (rows != 0){
            throw new RuntimeException("相册已存在！");
        }
        Date date = new Date();
        pmsAlbum.setGmtCreate(date);
        pmsAlbum.setGmtModified(date);
        return pmsAlbumMapper.insertPmsAlbum(pmsAlbum);
    }

    /**
     * 修改商品相册
     * 
     * @param pmsAlbum 商品相册
     * @return 结果
     */
    @Override
    public int updatePmsAlbum(PmsAlbum pmsAlbum)
    {
        String updateName = pmsAlbum.getName();
        if (StringUtils.isEmpty(updateName)) {
            throw new RuntimeException("修改的相册名称不能为空，请指定相册名称");
        }

        PmsAlbum album = pmsAlbumMapper.selectByUpdateName(updateName);
        if (album != null) {
            if (album.getName().equals(updateName) && album.getId() != pmsAlbum.getId()) {
                throw new RuntimeException("相册名称已存在，请重新指定相册名称");
            }
        }
        pmsAlbum.setGmtModified(new Date());
        return pmsAlbumMapper.updatePmsAlbum(pmsAlbum);
    }

    /**
     * 批量删除商品相册
     * 
     * @param ids 需要删除的商品相册主键
     * @return 结果
     */
    @Override
    public int deletePmsAlbumByIds(Long[] ids)
    {
        if (ids.length == 0){
            throw new RuntimeException("请选中要删除的数据");
        }
        return pmsAlbumMapper.deletePmsAlbumByIds(ids);
    }

    /**
     * 删除商品相册信息
     *
     * @param id 商品相册主键
     * @return 结果
     */
    @Override
    public int deletePmsAlbumById(Long id)
    {
        return pmsAlbumMapper.deletePmsAlbumById(id);
    }
}
